package com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.innodroid.expandablerecycler.ExpandableRecyclerAdapter;
import com.xiaoxing.zhinenghuacangku_zhijian.R;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.entity.QuYangLieBiao;

import java.util.List;
import java.util.Map;


public class QuYangLieBiaoAdapter extends ExpandableRecyclerAdapter<QuYangLieBiaoAdapter.OrderListItem> {
    public static final int TYPE_CANG_KU_NAME = 1000; //仓库名称
    public static final int TYPE_CANG_KU_PRODUCTS = 1001; //仓库商品名称
    public static final int TYPE_FOOTER = 1002; //商家订单底部统计
    String groupmid = "";
    String mid = "";
    private List<QuYangLieBiao> mGroups;
    private Map<String, List<QuYangLieBiao.MitemBean>> mChildren;
    private Map<Integer, Integer> mGroupsPosition;
    private CheckInterface checkInterface;
    private ChuKuInterface mChuKuInterface;
    private Handler handler;
    private Runnable runnable;

    public QuYangLieBiaoAdapter(Context context, List<QuYangLieBiao> groups, Map<String, List<QuYangLieBiao.MitemBean>> children, Map<Integer, Integer> groupsPosition) {
        super(context);
        this.mGroups = groups;
        this.mChildren = children;
        this.mGroupsPosition = groupsPosition;
        handler = new Handler();
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setChuKuInterface(ChuKuInterface chuKuInterface) {
        this.mChuKuInterface = chuKuInterface;
    }

    public Object getChild(int groupPosition, int childPosition) {
        List<QuYangLieBiao.MitemBean> childs = mChildren.get(String.valueOf(groupPosition));
        return childs.get(childPosition);
    }

    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_CANG_KU_NAME:
                return new ShangJiaNameViewHolder(inflate(R.layout.zhi_neng_cang_ku_zhi_jian_lie_biao_item_cang_ku_name, parent));
            case TYPE_FOOTER:
                return new FooterViewHolder(inflate(R.layout.zhi_neng_cang_ku_zhi_jian_lie_biao_item_cang_ku_footer, parent));
            case TYPE_CANG_KU_PRODUCTS:
            default:
                return new ProductsViewHolder(inflate(R.layout.zhi_neng_cang_ku_zhi_jian_lie_biao_item_cang_ku_products, parent));
        }
    }

    @Override
    public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_CANG_KU_NAME:
                ((ShangJiaNameViewHolder) holder).bind(position);
                break;
            case TYPE_FOOTER:
                ((FooterViewHolder) holder).bind(position);
                break;
            case TYPE_CANG_KU_PRODUCTS:
            default:
                ((ProductsViewHolder) holder).bind(position);
                break;
        }
    }

    public void setDataItems(List<OrderListItem> orderListItems) {
        setItems(orderListItems);
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    public interface ChuKuInterface {


        void chuKu(QuYangLieBiao quYangLieBiao);


    }

    public static class OrderListItem extends ExpandableRecyclerAdapter.ListItem {
        public QuYangLieBiao mQuYangLieBiao;
        public QuYangLieBiao mQuYangLieBiao1;
        public QuYangLieBiao.MitemBean mMitemBean;
        public int mGroupPosition;
        public int mChildPosition;
        public String mOutStoragelistBatchid;

        public OrderListItem(QuYangLieBiao quYangLieBiao) {
            super(TYPE_CANG_KU_NAME);
            mQuYangLieBiao = quYangLieBiao;
        }

        public OrderListItem(int groupPosition, QuYangLieBiao.MitemBean mitemBean, int childPosition, QuYangLieBiao quYangLieBiao1) {
            super(TYPE_CANG_KU_PRODUCTS);
            mMitemBean = mitemBean;
            mGroupPosition = groupPosition;
            mChildPosition = childPosition;
            mQuYangLieBiao1 = quYangLieBiao1;
        }

        public OrderListItem(String first, String last, String footer, String type) {
            super(TYPE_FOOTER);
        }
    }

    public class ShangJiaNameViewHolder extends ViewHolder {

        private TextView tv_qu_yang_dan_wei;
        private TextView tv_zhi_jian_ji_hua_id;
        private TextView tv_qu_yang_shu_liang;
        private TextView tv_wu_liao_ming_cheng;
        private TextView tv_chou_jian_lei_xing;
        private TextView tv_chou_jian_bian_hao;
        private TextView tv_chou_jian_ri_qi;
        private Button btn_chu_ku;

        public ShangJiaNameViewHolder(View view) {
            super(view);
            tv_qu_yang_dan_wei = view.findViewById(R.id.tv_qu_yang_dan_wei);
            tv_zhi_jian_ji_hua_id = view.findViewById(R.id.tv_zhi_jian_ji_hua_id);
            tv_qu_yang_shu_liang = view.findViewById(R.id.tv_qu_yang_shu_liang);
            tv_wu_liao_ming_cheng = view.findViewById(R.id.tv_wu_liao_ming_cheng);
            tv_chou_jian_lei_xing = view.findViewById(R.id.tv_chou_jian_lei_xing);
            tv_chou_jian_bian_hao = view.findViewById(R.id.tv_chou_jian_bian_hao);
            tv_chou_jian_ri_qi = view.findViewById(R.id.tv_chou_jian_ri_qi);
            btn_chu_ku = view.findViewById(R.id.btn_chu_ku);
        }

        public void bind(int position) {
            QuYangLieBiao quYangLieBiao = visibleItems.get(position).mQuYangLieBiao;
            tv_qu_yang_dan_wei.setText("取样单位：" + quYangLieBiao.getSpotunit());
            tv_zhi_jian_ji_hua_id.setText("计划ID：" + quYangLieBiao.getMonthid());
            tv_qu_yang_shu_liang.setText("取样数量：" + quYangLieBiao.getSpotnum());
            tv_wu_liao_ming_cheng.setText("物料名称：" + quYangLieBiao.getMaterial());
            tv_chou_jian_lei_xing.setText("抽检类型：" + quYangLieBiao.getSpottype());
            tv_chou_jian_bian_hao.setText("抽检编号：" + quYangLieBiao.getMsBH());
            tv_chou_jian_ri_qi.setText("抽检日期：" + quYangLieBiao.getPlandate());

//            final QuYangLieBiao group = (QuYangLieBiao) getGroup(mGroupsPosition.get(position));


            btn_chu_ku.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mChuKuInterface.chuKu(quYangLieBiao);
                }
            });


        }
    }

    public class ProductsViewHolder extends ViewHolder {

        private TextView tv_wu_liao_ming_cheng;
        private TextView tv_cang_wei;
        private TextView tv_cang_wei_id;
        private TextView tv_ku_cun_shu_liang;
        private TextView tv_mid;
        private TextView tv_xiang_mu_ming_cheng;
        private TextView tv_xiang_mu_bian_hao;
        private TextView tv_is_auto;
        private TextView tv_cang_ku;


        public ProductsViewHolder(View view) {
            super(view);
            tv_wu_liao_ming_cheng = view.findViewById(R.id.tv_wu_liao_ming_cheng);
            tv_cang_wei = view.findViewById(R.id.tv_cang_wei);
            tv_cang_wei_id = view.findViewById(R.id.tv_cang_wei_id);
            tv_ku_cun_shu_liang = view.findViewById(R.id.tv_ku_cun_shu_liang);
            tv_mid = view.findViewById(R.id.tv_mid);
            tv_xiang_mu_ming_cheng = view.findViewById(R.id.tv_xiang_mu_ming_cheng);
            tv_xiang_mu_bian_hao = view.findViewById(R.id.tv_xiang_mu_bian_hao);
            tv_is_auto = view.findViewById(R.id.tv_is_auto);
            tv_cang_ku = view.findViewById(R.id.tv_cang_ku);


        }

        @SuppressLint("LongLogTag")
        public void bind(int position) {
            try {
                QuYangLieBiao.MitemBean mitemBean = visibleItems.get(position).mMitemBean;
                QuYangLieBiao quYangLieBiao1 = visibleItems.get(position).mQuYangLieBiao1;
                tv_wu_liao_ming_cheng.setText("物料名称: " + mitemBean.getMaterial());
                tv_cang_wei.setText("仓位: " + mitemBean.getSpname());
                tv_cang_wei_id.setText("仓位ID: " + mitemBean.getSpid());
                tv_ku_cun_shu_liang.setText("库存数量: " + mitemBean.getNums());
                tv_mid.setText("核对序号: " + mitemBean.getMid());
                tv_xiang_mu_ming_cheng.setText("项目名称: " + mitemBean.getProjectName());
                tv_xiang_mu_bian_hao.setText("项目编号: " + mitemBean.getProid());
                tv_cang_ku.setText("仓库名称: " + mitemBean.getStorename());
                //{spotnum}{spotunit}{material}从{spname}放至{buffername}

                if (mitemBean.getIsauto().equals("1")) {//1 自动货架 0 人工仓位
                    tv_is_auto.setText("仓位类型: 自动货架");
                } else {
                    tv_is_auto.setText("仓位类型: 人工仓位");
                }


//                int groupPosition = visibleItems.get(position).mGroupPosition;
//                int childPosition = visibleItems.get(position).mChildPosition;
//                final QuYangLieBiao.MitemBean goodsInfo2 = (QuYangLieBiao.MitemBean) getChild(groupPosition, childPosition);

            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public class FooterViewHolder extends ViewHolder {

        private Button btn_que_ding;

        public FooterViewHolder(View view) {
            super(view);
            btn_que_ding = view.findViewById(R.id.btn_que_ding);
        }

        public void bind(int position) {
            btn_que_ding.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    mChuKuInterface.chuKu("");
                }
            });
        }
    }
//    public void shuas(){
//        mGroups=new ArrayList<>();
//        notifyDataSetChanged();
//    }
}
